package com.hulk.dryad.web.service;

import cn.ms.sequence.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.constant.enums.BEC;
import com.hulk.dryad.common.exception.BaseRteException;
import com.hulk.dryad.common.exception.WebRteException;
import com.hulk.dryad.persistence.entity.CustomerLoginInfo;
import com.hulk.dryad.persistence.entity.CustomerOperator;
import com.hulk.dryad.persistence.service.impl.CustomerLoginInfoServiceImpl;
import com.hulk.dryad.persistence.service.impl.CustomerOperatorServiceImpl;
import com.hulk.dryad.web.api.vo.CustomerLoginRespVO;
import com.hulk.dryad.web.api.vo.CustomerPwdLoginReqVO;
import com.hulk.dryad.web.api.vo.CustomerSMSLoginReqVO;
import com.hulk.dryad.web.api.vo.CustomerSocialLoginReqVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author lqx
 */

@Slf4j
@Component
@AllArgsConstructor
public class CustomerServiceImpl {

    private CustomerLoginInfoServiceImpl customerLoginInfoServiceImpl;
    private CustomerOperatorServiceImpl customerOperatorServiceImpl;


    /**
     * <post>用户密码登陆</post>
     *
     * @param reqDto 用户登陆
     * @return R
     */

    @PrintlnLog
    public CustomerLoginRespVO loginPostByPassword( CustomerPwdLoginReqVO reqDto) {
        String workId = IdWorker.getId() + "";
        log.info("workId:[{}] 用户密码登陆,入参:[{}]", workId, reqDto);
        CustomerLoginRespVO respDto;
        try {
            CustomerLoginInfo customer = customerLoginInfoServiceImpl.getOne(Wrappers.<CustomerLoginInfo>lambdaQuery()
                    .eq(CustomerLoginInfo::getLoginNo, reqDto.getLoginNo()).eq(CustomerLoginInfo::getLoginType, reqDto.getLoginType()));
            if (null == customer) {
                throw new WebRteException(BEC.E_2015);
            }
            CustomerOperator operator = customerOperatorServiceImpl.getOne(Wrappers.<CustomerOperator>lambdaQuery()
                    .eq(CustomerOperator::getOperatorNo, customer.getOperatorNo()));
            if (null == operator) {
                throw new WebRteException(BEC.E_2015);
            }
            //check_password // TODO: 2020/10/13

            //init-resp
            respDto = new CustomerLoginRespVO();
            respDto.setOperatorNo(operator.getOperatorNo());
            respDto.setLoginPwd(operator.getLoginPwd());
            respDto.setNickName(operator.getNickName());
			respDto.setAvatar(operator.getAvatar());
        } catch (BaseRteException ex) {
            log.error("workId:[{}] 用户密码登陆,异常:[{}]", workId, ex);
            throw ex;
        } catch (Exception e) {
            log.error("workId:[{}] 用户密码登陆,异常:[{}]", workId, e);
            throw new WebRteException(BEC.E_9999);
        }
        log.info("workId:[{}] 用户密码登陆,返回:[{}]", workId, respDto);
        return respDto;
    }

    /**
     * <post>用户验证码登陆</post>
     *
     * @param reqDto 用户登陆
     * @return R
     */
    @PrintlnLog
    @Idempotent
    public CustomerLoginRespVO loginPostBySMS( CustomerSMSLoginReqVO reqDto) {
        String workId = IdWorker.getId() + "";
        log.info("workId:[{}] 用户验证码登陆,入参:[{}]", workId, reqDto);
        CustomerLoginRespVO respDto;
        try {
            CustomerLoginInfo customer = customerLoginInfoServiceImpl.getOne(Wrappers.<CustomerLoginInfo>lambdaQuery()
					.eq(CustomerLoginInfo::getLoginNo, reqDto.getLoginNo()).eq(CustomerLoginInfo::getLoginType, reqDto.getLoginType()));
            if (null == customer) {
                throw new WebRteException(BEC.E_2015);
            }
            CustomerOperator operator = customerOperatorServiceImpl.getOne(Wrappers.<CustomerOperator>lambdaQuery()
                    .eq(CustomerOperator::getOperatorNo, customer.getOperatorNo()));
            if (null == operator) {
                throw new WebRteException(BEC.E_2015);
            }
            //check_smsCode // TODO: 2020/10/10
            //SmsSendRecord

            //init-resp
            respDto = new CustomerLoginRespVO();
            respDto.setOperatorNo(operator.getOperatorNo());
            respDto.setLoginPwd(operator.getLoginPwd());
            respDto.setNickName(operator.getNickName());
			respDto.setAvatar(operator.getAvatar());
        } catch (BaseRteException ex) {
            log.error("workId:[{}] 用户验证码登陆,异常:[{}]", workId, ex);
            throw ex;
        } catch (Exception e) {
            log.error("workId:[{}] 用户验证码登陆,异常:[{}]", workId, e);
            throw new WebRteException(BEC.E_9999);
        }
        log.info("workId:[{}] 用户验证码登陆,返回:[{}]", workId, respDto);
        return respDto;
    }


    /**
     * social
     */
    @PrintlnLog
    public CustomerLoginRespVO loginPostBySocial( CustomerSocialLoginReqVO reqDto) {
        String workId = IdWorker.getId() + "";
        log.info("workId:[{}] 用户社交登陆,入参:[{}]", workId, reqDto);
        CustomerLoginRespVO respDto = new CustomerLoginRespVO();
		respDto.setDeptId(0);
        log.info("workId:[{}] 用户社交登陆,返回:[{}]", workId, respDto);
        return respDto;
    }

    /**
     * loginRefresh
     * @param loginNo loginNo
     * @param loginType loginType
     * @return R
     */
    @PrintlnLog
    public CustomerLoginRespVO refresh( String loginNo,String loginType) {
		CustomerLoginInfo loginInfo = customerLoginInfoServiceImpl.getOne(Wrappers.<CustomerLoginInfo>lambdaQuery().eq(CustomerLoginInfo::getLoginNo,loginNo
		).eq(CustomerLoginInfo::getLoginType,loginType));
        CustomerOperator operator = customerOperatorServiceImpl.getOne(Wrappers.<CustomerOperator>lambdaQuery()
                .eq(CustomerOperator::getOperatorNo, loginInfo.getOperatorNo()));
        if (null == operator) {
            throw new WebRteException(BEC.E_2015);
        }
        CustomerLoginRespVO respDto = new CustomerLoginRespVO();
        respDto.setOperatorNo(operator.getOperatorNo());
        respDto.setLoginPwd(operator.getLoginPwd());
        respDto.setNickName(operator.getNickName());
		respDto.setAvatar(operator.getAvatar());
        return respDto;
    }



}